import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
	//检查process.cwd()路径下.env.development.local、.env.development、.env.local、.env这四个环境文件
	const env = loadEnv(mode, process.cwd()); // 获取.env文件里定义的环境变量

	return {
		plugins: [vue()],
		resolve: {
			// 别名
			alias: {
				"@": "/src",
				"vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js",
			},
			extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
		},
		build: {
			rollupOptions: {
				// external: ["lodash", "axios"],
			},
		},
		server: {
			port: env.VITE_APP_PORT,
			host: true,
			open: true,
			proxy: {
				"/api": {
					// 配置需要代理的路径 --> 这里的意思是代理http://localhost:80/api/后的所有路由
					target: env.VITE_APP_API_BASEURL, // 目标地址 --> 服务器地址
					changeOrigin: true, // 允许跨域
					ws: true, // 允许websocket代理
					// 重写路径 --> 作用与vue配置pathRewrite作用相同
					rewrite: (path) => path.replace(/^\/api/, ""),
				},
			},
		},
		css: {
			postcss: {
				plugins: [
					{
						postcssPlugin: "internal:charset-removal",
						AtRule: {
							charset: (atRule) => {
								if (atRule.name === "charset") {
									atRule.remove();
								}
							},
						},
					},
				],
			},
		},
	};
});
